<template>
  <div class="report-wrapper">
    <!-- <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container"> -->
    <div class="left-content">
      <span class="text">数据上报类型</span>
    </div>
    <div class="right-content">
      <!-- <div class="header">
        <el-radio-group v-model="postForm.reportType">
          <el-radio label="文件上传">文件上传</el-radio>
          <el-radio label="单行填报">单表填报(单行)</el-radio>
          <el-radio label="多行填报">表格填报(多行)</el-radio>
          <el-radio label="测评问卷">测评/问卷</el-radio>
        </el-radio-group>
      </div> -->
      <!-- 文件上报 -->
      <!-- <div class="fileUp" v-if="postForm.reportType === '文件上传'">
        规定上报文件数
        <el-input v-model="postForm.fileNum" type="text" size="small" style="width:200px" class="article-textarea" placeholder="文件数量" />
        (如不需要限制上报文件数量，留空或者设置为0即可)
      </div> -->
      <!-- 单表填报 -->
      <div class="singleTable table" v-if="reportType === '表格填报'">
        <span class="desc">一个用户可以填写一个表格的{{tableType === '单行' ? '一行': '多行'}}数据</span>
        <div class="write-table">
          <!-- <div class="title">表头设计</div> -->
          <div class="taleInfo">
            <el-form ref="postForm" class="form-container">
              <el-form-item label-width="80px" class="postInfo-container-item" label="表格名称">
                <el-input v-model="tableTitle" size="small" placeholder="输入表格标题" />
              </el-form-item>
              <el-form-item label-width="80px" class="postInfo-container-item" label="表格类型">
                <el-radio-group v-model="tableType">
                  <el-radio label="单行">单行</el-radio>
                  <el-radio label="多行">多行</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-form>
          </div>
          <el-divider></el-divider>
          <div class="items" v-for="(item,index) in singleTable">
            表项名
            <el-input v-model="item.name" type="text" size="small" class="item-name" />
            <el-select v-model="item.status" size="small" class="item-type" v-if="!item.child.length">
              <el-option label="必填" value="必填"></el-option>
              <el-option label="非必填" value="非必填"></el-option>
            </el-select>
            <el-button icon="el-icon-plus" circle size="small" @click="handAddTableItem(2, index)"></el-button>
            <el-button type="danger" icon="el-icon-close" circle size="small" style="margin-left:0" @click="handDelTableItem(1, index)"></el-button>
              <div class="subItem"  v-for="(subItem,subIndex) in item.child">
                子项名
                <el-input v-model="subItem.name" type="text" size="small" class="item-name" />
                <el-select v-model="subItem.status" size="small" class="item-type">
                  <el-option label="必填" value="必填"></el-option>
                  <el-option label="非必填" value="非必填"></el-option>
                </el-select>
                <el-button icon="el-icon-plus" circle size="small" @click="handAddTableItem(3, index, subIndex)"></el-button>
                <el-button type="danger" icon="el-icon-close" circle size="small" style="margin-left:0" @click="handDelTableItem(2, index, subIndex)"></el-button>
                <template v-if="subItem.child.length">
                  <div class="childItem"  v-for="(childItem, childIndex) in subItem.child">
                    子项名
                    <el-input v-model="childItem.name" type="text" size="small" class="item-name" />
                    <el-select v-model="childItem.status" size="small" class="item-type">
                      <el-option label="必填" value="必填"></el-option>
                      <el-option label="非必填" value="非必填"></el-option>
                    </el-select>
                    <!-- <el-button icon="el-icon-plus" circle size="small" @click="addSubPram(index)"></el-button> -->
                    <el-button type="danger" icon="el-icon-close" circle size="small" style="margin-left:0" @click="handDelTableItem(3, index, subIndex, childIndex)"></el-button>
                  </div>
                </template>
              </div>
          </div>
          <div class="addBtn">
            <el-button icon="el-icon-plus" size="small" @click="handAddTableItem(1)">添加表项</el-button>
          </div>
          <!-- <el-button icon="el-icon-plus" @click="handCreateTable">添加</el-button> -->
        </div>
      </div>
      <!-- 问卷填写 -->
      <div class="questPage table" v-if="reportType === '测评问卷'">
        <div class="write-table">
          <div class="title">问卷设计</div>
          <el-divider></el-divider>
          <div class="input-wraper">
            <span style="min-width:78px;line-height:2;">问卷标题</span>
            <el-input v-model="questionName" placeholder="输入问卷标题" type="text" size="small"/>
          </div>
          <el-divider></el-divider>
          <el-radio-group v-model="isFade">
            <el-radio label="False">实名问卷(需要登陆)</el-radio>
            <el-radio label="True">匿名问卷(无需登陆)</el-radio>
          </el-radio-group>
          <el-divider></el-divider>
          <div class="items" v-for="(item,index) in question" :key="index">
                <div class="question-header">
                    <!-- 编号 
                    <el-input v-model="item.num" type="text" size="small" class="item-num" /> -->
                    {{ index + 1}}、问题描述
                    <el-input v-model="item.title" type="text" size="small" class="item-name" />
                    <el-select v-model="item.qtype" size="small" class="item-opType">
                        <el-option label="单选" value="单选"></el-option>
                        <el-option label="多选" value="多选"></el-option>
                        <el-option label="填空" value="填空"></el-option>
                    </el-select>
                    <el-button v-if="question.length > 1" icon="el-icon-minus" circle size="small" style="margin-left:0" @click="delQuesPram(0, index)"></el-button>
                </div>
                <div class="question-option" v-show="item.qtype != '填空'">
                  <div class="option-items" v-for="(opt, oindex) in item.choice">
                        <span>选项{{ oindex +1}}</span>
                        <el-input v-model="item.choice[oindex]" type="text" size="small" class="item-answer"/>
                        <el-button icon="el-icon-plus" circle size="small" @click="addOption(index)"></el-button>
                        <el-button type="danger" icon="el-icon-minus" circle size="small" style="margin-left:0" @click="delQuesPram(1, index, oindex)"></el-button>
                    </div>
                </div>
              </div>
          <div class="addBtn">
            <el-button icon="el-icon-plus" size="small" @click="handAddQuestion">添加选项</el-button>
          </div>
          <!-- <el-button icon="el-icon-plus" @click="handCreteQuestion">提交</el-button> -->
        </div>
      </div>

    </div>
  </div>
      <!-- <el-row type="flex" align="middle" style="padding:10px 0 20px 80px" v-if="postForm.reportType === '文件上传'">
        <span>关联指标</span>
        <el-switch style="margin-left:54px;"
            v-model="isRelate">
          </el-switch>
        <treeselect v-if="isRelate" placeholder="请选择指标" style="width: 300px;margin-left:10px;" v-model="markVaule" :options="markOptions" @select="handSelectMark" />
      </el-row> -->
</template>
<script>
  import { createQuestion, createTableReport, getTable } from '@/api/question.js'
  import {
    postCreateTask,
    getIndexList,
    delTask
  } from '@/api/work.js'
   // import the component
   import Treeselect from '@riophae/vue-treeselect'
  // import the styles
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  export default {
    name: 'reportType',
    components:{
      Treeselect
    },
    props: {
      reportType: {
        type: String
      },
      reportTitle:{
        type: String,
        default: ''
      }
    },
    watch: {
      reportTitle(str){
        if(str && !this.tableTitle){
          this.tableTitle = str
        }
      }
    },
    mounted() {
      this.requestTableInfo()
    },
    data() {
      return {
        // markVaule: null,
        // markOptions: [],-
        // isRelate: false,
        // relateMark:'',
        // normalizer(node) {
        //   return {
        //     id: node.id,
        //     label: node.name,
        //     children: node.child,
        //   }
        // },
        postForm: {
          reportType: '文件上传',
          type: '必填',
          fileNum:0
        },
        tableType: '单行',
        tableTitle: '',
        singleTable: [
          { 
            name: '',
            status:'必填',
            child:[]
          },
          {
            name: '',
            status:'必填',
            child:[]
          }
        ],
        isFade:'False',
        questionName:'',
        question: [{
            title: '',
            qtype: '单选',
            choice: [''],
        }]
      }
    },
    methods: {
      // 选择指标
      handSelectMark(node, id) {
      },
      addOption(index) {
        this.question[index].choice.push('')
      },
      /*
      * 表格填报增加项
      * params:
      *   type: str, 子项层级类别
      *   index: str, 第一级索引
      *   sunIndex: str, 第二级索引
      *   childIndex: str, 第三级索引
      */
      handAddTableItem(type = 1, index = 0, sunIndex = 0, childIndex = 0){
        switch(type){
          case 1:
            this.singleTable.push(
              { 
                name: '',
                status:'必填',
                child:[]
              })
            break;
          case 2:
            this.singleTable[index].child.push(
              { 
                name: '',
                status:'必填',
                child:[]
              })
            break;
          case 3:
            this.singleTable[index].child[sunIndex].child.push(
              { 
                name: '',
                status:'必填',
                child:[]
              })
            break;
        }
      },
      /*
      * 表格填报删除项
      * params:
      *   type: str, 子项层级类别
      *   index: str, 第一级索引
      *   sunIndex: str, 第二级索引
      *   childIndex: str, 第三级索引
      */
      handDelTableItem(type = 1, index = 0, sunIndex = 0, childIndex = 0){
        this.$confirm('是否确定删除表格项？', '警告', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
          switch (type) {
            case 1:
              this.singleTable.splice(index , 1)
              break
            case 2:
              this.singleTable[index].child.splice(sunIndex, 1);
              break
            case 3:
              this.singleTable[index].child[sunIndex].child.splice(childIndex, 1);
              break
        }
        }).catch(() => {
           
        })
      },
      // 删除问卷子项
      delQuesPram(type, index, temp){
        switch(type){
          case 0:
            this.question.length > 1 && this.question.splice(index, 1)
            break;
          case 1:
            this.question[index].choice.length > 1 && this.question[index].choice.splice(temp, 1)
            break;
        }
      },
      handAdd() {
        this.singleTable.push(
          { 
            name: '',
            status:'必填',
            child:[]
          })
      },
      handAddQuestion(){
          this.question.push({
            title: '',
            qtype: '单选',
            choice: [''],
        })
      },
      // fetchMarkList() {
      //   getIndexList({parent: 'null'}).then(res =>{
      //     this.markOptions = this.formatData(res.data.results)
      //   })
      // },
      formatData(data){
        const res = []
        data.forEach(e =>{
          e.label = e.name
          e.children = e.child
          if(e.children.length){
            e.children.forEach(item =>{
              item.label = item.name
              item.children = item.child
            })
          }
        })
        return data
      },
      handCreateTable(){
        let type = '单行'
        if(this.postForm.reportType === '多行填报'){
          type = '多行'
        }
        const para = {
          task: 220,
          type: this.tableType,
          title: this.tableTitle,
          item: this.singleTable
        }
        createTableReport(para).then(res=>{

        })
      },
      handCreteQuestion(){
        const para ={
          task:1,
          caption:'我的问卷',
          item:this.question
        }
        createQuestion(para).then(res=>{
        })
      },
      requestTableInfo(){
        getTable().then()
      }
    },
  }

</script>
<style lang="scss">
  .report-wrapper {
    position: relative;
    padding: 20px 80px 20px;
    display: flex;
    font-size: 15px;
    color: #606266;
    .left-content {
      display: flex;
      align-items: center;
      margin-right: 20px;
    }

    .right-content {
      .header {
        margin-bottom: 24px;
      }

      .table {
        .desc {
        }

        .write-table {
          width: 850px;
          min-height: 600px;
          margin-top: 10px;
          padding: 0 10px 70px;
          border: 1px solid #ccc;
          border-radius: 8px;
          position: relative;
          .taleInfo{
            margin-top: 10px;
            .el-form-item{
              margin-bottom: 0;
              .el-form-item__label{
                font-weight: normal;
                font-size: 15px;
                color: #606266;
              }
            }
          }
          .title {
            text-align: center;
            padding: 10px 0;
          }
          .input-wraper{
            display: flex;
          }
          .items {
            margin: 0 10px;
            padding:10px 0;
            border-bottom: 1px solid #ccc;

            .item-name {
              margin-left: 10px;
              width: 350px;
            }

            .item-type {
              width: 120px;
            }
            .subItem{
              margin: 6px 0 0 25px;
              &::before{
                content: '';
                border: 1px solid;
                border-top: none;
                border-right: none;
                margin-bottom: 10px;
                padding-right: 32px;
              }
              .childItem{
                margin: 6px 0 0 62px;
                &::before{
                content: '';
                border: 1px solid;
                border-top: none;
                border-right: none;
                margin-bottom: 10px;
                padding-right: 32px;
              }
              }
            }
          }

          .addBtn {
            position: absolute;
            bottom: 10px;
            margin: 0 auto;
            left: 0;
            right: 0;
            text-align: center;

          }
        }
      }
      .questPage{
          .items{
            .item-num{
                width: 50px;
            }
            .item-opType{
                width: 80px;
            }
            .question-option{
                margin-top: 10px;
                margin-left: 57px;
                span{
                    display:inline-block;
                    min-width: 36px;
                    line-height: 1.9;
                }
                .option-items{
                  .item-answer{
                    width: 360px;
                    padding:0 10px 5px 0;
                   
                  } 
                }
            }
          }
      }
    }
  }

  .article-textarea /deep/ {
    width: 200px;
  }

</style>
<style>
.el-divider--horizontal{
  margin: 10px 0!important;
}
.vue-treeselect__label{
  font-weight: normal;
}
</style>